package com.youzikj.wx.service;

import java.util.Date;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.youzikj.wx.comm.respmsg.RespTextMessage;
import com.youzikj.wx.comm.subscribe.SubscribeInfoUtil;
import com.youzikj.wx.comm.subscribe.pojo.WeixinUserInfo;
import com.youzikj.wx.comm.util.CommonUtil;
import com.youzikj.wx.comm.util.MessageUtil;
import com.youzikj.wx.controller.ValidateController;
import com.youzikj.wx.core.pojo.Pub;
import com.youzikj.wx.core.pojo.Sub;
import com.youzikj.wx.core.service.PubServiceImpl;
import com.youzikj.wx.core.service.SubServiceImpl;

/**
 * 消息核心服务类
 *
 */
@Controller
public class MsgCoreService {
	
	private static final Logger log = Logger.getLogger(MsgCoreService.class);
	
	@Autowired
	private SubServiceImpl subService_;
	
	@Autowired
	private PubServiceImpl pubService_;
	
	@PostConstruct
	public void init() {
		MsgCoreService.subService = subService_;
		MsgCoreService.pubService = pubService_;
	}
	
	private static SubServiceImpl subService;
	
	private static PubServiceImpl pubService;
	
	
	/**
	 * 处理微信发来的请求
	 */
	public static String processRequest(HttpServletRequest request){
		
		String respXml = null;
		//默认返回文本内容
		String respContent = "未知消息类型";
		try {
			Map<String, String> requestMap = MessageUtil.parseXml(request);
			String fromUserName = requestMap.get("FromUserName");
			String toUserName = requestMap.get("ToUserName");
			String msgType = requestMap.get("MsgType");
			log.info("fromUserName:"+fromUserName+"toUserName"+toUserName+"msgType"+msgType);
			//回复文本消息
			RespTextMessage textMessage = new RespTextMessage();
			textMessage.setFromUserName(toUserName);
			textMessage.setToUserName(fromUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
			if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)){
				log.info("文本消息==========");
				respContent = "您发送的是文本消息";
			}else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)){
				respContent = "您发送的是图片消息";
			} else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)){
				respContent = "您发送的是语音消息";
			} else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VIDEO)){
				respContent = "您发送的是视频消息";
			} else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)){
				respContent = "您发送的是地理位置消息";
			} else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)){
				respContent = "您发送的是链接消息";
			}else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)){
				//事件类型
				String eventType = requestMap.get("Event");
				//关注
				if(eventType.equals(MessageUtil.EVENT_MESSAGE_TYPE_SUBSCRIBE)){
					respContent = "谢谢您的关注";
					WeixinUserInfo userInfo = SubscribeInfoUtil.getUserInfo(CommonUtil.getSingToken(CommonUtil.appId, CommonUtil.appSecret), fromUserName);
					Sub sub = new Sub();
					sub.setNickname(userInfo.getNickname());
					sub.setHead_url(userInfo.getHeadImgUrl());
					sub.setOpenid(userInfo.getOpenId());
					try {
						log.info("openid:"+userInfo.getOpenId()+" nickname:"+userInfo.getNickname()+" subscribe:"+userInfo.getSubscribe()+" headIamge"+userInfo.getHeadImgUrl());
						subService.addSub(sub);
					} catch (Exception e) {
						log.info("添加关注者信息失败"+e);
					}
					Pub pub  = new Pub();
					pub.setHead_url(userInfo.getHeadImgUrl());
					pub.setNickname(userInfo.getNickname());
					pub.setOpenid(userInfo.getOpenId());
					try {
						pubService.addPub(pub);
					} catch (Exception e) {
						log.info("添加发布者信息失败"+e);
					}
					
					
				} else if(eventType.equals(MessageUtil.EVENT_MESSAGE_TYPE_UNSUBSCRIBE)){
					//取消订阅，用户已收不到消息
				} else if(eventType.equals(MessageUtil.EVENT_MESSAGE_TYPE_SCAN)){
					 //处理扫描二维码事件
					
				} else if(eventType.equals(MessageUtil.EVENT_MESSAGE_TYPE_LOCATION)){
					//处理上报位置事件
				} else if(eventType.equals(MessageUtil.EVENT_MESSAGE_TYPE_CLICK)){
					//处理自定义菜单事件
					
					//事件key值，与创建菜单时的key对应
					String eventKey = requestMap.get("EventKey");
					//根据key判断用户点击的按钮
					if(eventKey.equals("oschina")){
						textMessage.setContent("hello word");
						respXml = MessageUtil.messageToXml(textMessage);
						return respXml;
					} else if(eventKey.equals("iteye")){
						textMessage.setContent("你好");
						respXml = MessageUtil.messageToXml(textMessage);
						return respXml;
					}
				}
			}
			textMessage.setContent(respContent);
			//转换成xml
			respXml = MessageUtil.messageToXml(textMessage);
		} catch (Exception e) {
			e.printStackTrace(); 
		}
		return respXml;
	}
}
